<?php
namespace Destroyer\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\ValidationException;
use Illuminate\Validation\Rule;
use Destroyer\Models\Role;

class RoleCreateRequest extends FormRequest
{

    protected $stopOnFirstFailure = false;

    public function rules(): array
    {
        $unique = Rule::unique(Role::class);

        return [
            'name'     => ['required', 'alpha_num', 'between:2,50', $unique],
            'code'     => ['required', 'alpha_num:ascii', 'between:2,50', $unique],
            'sort'     => ['required', 'integer', 'between:0,65535'],
            'status'   => ['required', 'boolean'],
            // 附加字段
            'menu_ids'   => ['nullable', 'array'],
            'menu_ids.*'   => ['sometimes', 'integer', 'exists:system_menu,id'],
        ];
    }

    public function attributes()
    {
        return [
            'name'   => '角色名称',
            'code'   => '权限字符',
            'sort'   => '排序',
            'status' => '状态',
        ];
    }

    public function messages()
    {
        return [
            'name.unique' => ':attribute `:input` 已经存在',
            'code.unique' => ':attribute `:input` 已经存在',
        ];
    }
}
